package com.mowh.gof23.behavioral.patterns.visitor;

import java.util.Iterator;

/**
 * @author mowh1gd
 * @create 2023/10/20 10:13
 */
public class ListVisitor extends Visitor{
    private String currentDir = "";
    @Override
    public void visit(File file) {
        System.out.println(currentDir + "/" + file);
    } //注意递归的结束

    @Override
    public void visit(Directory directory) {
        System.out.println(currentDir + "/" + directory);
        String saveDir = currentDir;
        currentDir = currentDir + "/" + directory.getName();
        Iterator it = directory.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            entry.accept(this); //注意原本数据结构是递归的，所以出现调用Directory.accept()->visitor.visit()->entry.accept()->visitor.visit()->entry.accept()...
        }
        currentDir = saveDir;
    }
}
